﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace DataAccess
{
    public class DataAccessCommand
    {
        private IDbCommand command;
        private object filter;

        internal IDbCommand Command
        {
            get { return command; }
        }
        public object Filter
        {
            get { return filter; }
            set
            { 
                filter = value;
                SetFilter();
            }
        }

        /// <summary>
        ///Constructor del Objeto Commando, predeterminadamente sera un store procedure sin parametros de entrada ni salida
        /// </summary>
        /// <returns>Un IDbCommand para ejecutar en base de datos, ya trae la conexion asociada</returns>
        public DataAccessCommand(string textCommand)
        {
            DataConection connection = new DataConection();
            command = connection.CreateDBCommand();

            command.CommandText = textCommand;
            command.CommandType = CommandType.StoredProcedure;
        }

        public DataAccessCommand(string textCommand, CommandType type)
        {
            DataConection connection = new DataConection();
            command = connection.CreateDBCommand();

            command.CommandText = textCommand;
            command.CommandType = type;
        }

        public DataAccessCommand(string textCommand, object filter)
        {
            DataConection connection = new DataConection();
            command = connection.CreateDBCommand();

            command.CommandText = textCommand;
            command.CommandType = CommandType.StoredProcedure;

            Filter = filter;
        }

        public DataAccessCommand(string textCommand, CommandType type, object filter)
        {
            DataConection connection = new DataConection();
            command = connection.CreateDBCommand();

            command.CommandText = textCommand;
            command.CommandType = type;

            Filter = filter;
        }

        public void SetParamater(string nameParameter, Type typeParameter, object value)
        {
            IDataParameter parameter;

            //Se evalua si el parametro existe
            if (command.Parameters.Contains(nameParameter))
                parameter = command.Parameters[nameParameter] as IDataParameter;
            else
                parameter = command.CreateParameter();

            parameter.DbType = DataAccessHelper.GetDBType(typeParameter);
            parameter.ParameterName = nameParameter;
            parameter.Value = value;

            command.Parameters.Add(parameter);            
        }

        private void SetFilter()
        {
            foreach (PropertyInfo info in Filter.GetType().GetProperties())
            {
                if (info.GetCustomAttributes(false).Count() > 0)
                {
                    DataAccessParameter parametro = info.GetCustomAttributes(typeof(DataAccessParameter), false)[0] as DataAccessParameter;

                    SetParamater(parametro.NameParameter, info.PropertyType, info.GetValue(Filter, null));
                }
            }
        }
    }
}
